<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>解决Promise类then函数pending状态处理</title>
  <script src="./promise.js"></script>
</head>
<body>
  <ul>
    <li>通过在pending状态下将then函数回调onResolved或onRejected加入到callbacks中，在resolve或reject真正执行触发对应的回调</li>
    <li>此时在then函数回调onResolved或onRejected出现异常又无法捕获(因为走pending状态处理了)，因此还需要通过处理callbacks异常捕获</li>
    <li>此时在pending状态下延迟仍然出现异步问题，正常应该是执行setTimeout内部同步代码再执行异步，通过将resolve或reject中将callbacks加入到宏任务setTimeout完成异步</li>
    <li>此时then函数返回的是undefined，而正常是返回Promise且可以链式操作(未解决)</li>
  </ul>
  <script>
    // main -> promise -> success
    const promise = new MyPromise((resolve, reject) => {
      setTimeout(() => {
        resolve('success')
        // reject('fail')
        console.log('promise')
      }, 1000)
    }).then(res => console.log(res), err => console.log(err))
    console.log('main')
    console.log(promise) // undefined
  </script>
</body>
</html>